package data
{
	import data.events.DataEvent;
	import data.events.SocketBaseEvent;
	import data.request.RequestData;
	import data.socket.SocketData;
	
	import flash.events.EventDispatcher;

	public class DataFactory extends EventDispatcher
	{
		/**
		 * Socket 数据通讯类 
		 */		
		private var _socketData:SocketData;
		
		/**
		 * Request通讯
		 */		
		private var _requestData:RequestData;
		
		private var _dataSend:DataSend;
		private var _dataReceive:DataReceive;
		private var _isSocketRegisterComplete:Boolean;//Socket数据通讯类型是否注册成功
		
		public function DataFactory()
		{
			_socketData=new SocketData();
			_requestData=new RequestData();
		}
		
		private static var _instance:DataFactory;
		public static function get Instance() : DataFactory
		{
			if(_instance == null)
			{
				_instance = new DataFactory();
			}
			return _instance;
		}
		
		/**
		 *通讯工厂注册 
		 */		
		public function register(host:String, port:uint):void
		{
			_socketData.addEventListener(SocketBaseEvent.REGISTER_COMPLETE, socketRegisterComplete);//Socket通讯注册完成侦听
			_socketData.connect(host, port);//Socket通讯工厂注册
		}
		
		/**
		 * Socket通讯工厂注册完成
		 */		
		private function socketRegisterComplete(evt:SocketBaseEvent):void
		{
			_isSocketRegisterComplete=true;
			dataRegisterCompleteCheck();
		}
		
		/**
		 *数据通讯是否全部注册成功检查 
		 */		
		private function dataRegisterCompleteCheck():void
		{
			if(_isSocketRegisterComplete)
			{
				_dataSend=new DataSend();
				_dataReceive=new DataReceive();
				dispatchEvent(new DataEvent(DataEvent.DATA_FACTORY_REGISTER_COMPLETE, true));
			}
		}
		
		/**
		 *Socket数据通讯类
		 */		
		public function get socketData():SocketData
		{
			return _socketData;
		}
		
		/**
		 * Request通讯
		 */		
		public function get requestData():RequestData
		{
			return _requestData;
		}
		
		/**
		 * 数据发送对象
		 */		
		public function get dataSend():DataSend
		{
			return _dataSend;
		}
		
		/**
		 *数据返回对象
		 */		
		public function get dataReceive():DataReceive
		{
			return _dataReceive;
		}
	}
}